package org.xt.module.api;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.xt.constants.Const;
import org.xt.constants.Res;
import org.xt.entity.DataMachine;
import org.xt.entity.User;

@IocBean
@At("/api/dataMachine")
public class DataMachineApiModule {
	@Inject
	private Dao dao;
	
	@At("/list")
	@Ok("json")
	public Res list(@Param(value="page", df="1")int page, @Param(value="size", df="10")int size){
		Pager pager = dao.createPager(page, size);
		pager.setRecordCount(dao.count(DataMachine.class, null));
		return Res.NEW()
				.code(Res.SUCCESS)
				.msg("ok")
				.data(new QueryResult(dao.query(DataMachine.class, Cnd.NEW().asc("sort"), pager), pager));
	}
	
	@At("/findById")
	@Ok("json")
	public Res findById(int id){
		DataMachine data = dao.fetch(DataMachine.class, id);
		if(data != null){
			return Res.NEW().code(Res.SUCCESS).msg("获取OK").data(data);
		}else{
			return Res.NEW().code(Res.ERROR).msg("获取失败");
		}
	}
	@At("/add")
	@Ok("json")
	public Res add(@Param("..")DataMachine data){
		data.setCreateTime(new Date());
		data.setUpdateTime(new Date());
		data = dao.insert(data);
		if(data != null){
			return Res.NEW().code(Res.SUCCESS).msg("添加OK").data(data);
		}else{
			return Res.NEW().code(Res.ERROR).msg("添加失败");
		}
	}
	
	@At("/update")
	@Ok("json")
	public Res update(@Param("..")DataMachine data){
		data.setUpdateTime(new Date());
		if(dao.updateIgnoreNull(data) > 0){
			return Res.NEW().code(Res.SUCCESS).msg("更新OK");
		}else{
			return Res.NEW().code(Res.ERROR).msg("更新失败");
		}
	}
	@At("/delete")
	@Ok("json")
	public Res delete(int id){
		if(dao.delete(DataMachine.class, id) > 0){
			return Res.NEW().code(Res.SUCCESS).msg("删除OK");
		}else{
			return Res.NEW().code(Res.ERROR).msg("删除失败");
		}
	}
}
